#include "../include/backup.h"
#include "../include/sync.h"

int main() {
    int task;
    int num_task;

    printf("==>输入你想要执行的任务种类\n");
    printf("1. backup\n2. sync\n");
    scanf("%d", &task);
    
    if (task == 1) { //backup
        printf("==>输入想要的【备份】任务的个数\n");
        scanf("%d", &num_task);
        printf("按行输入(空格分隔)要执行的【备份】任务的参数【源目录路径，目标目录路径】\n");
        BackupTask tasks[num_task];
        for (int i = 0 ; i < num_task; i++) {
            scanf("%s %s", tasks[i].source, tasks[i].destination);
            fflush(stdin);
            printf("【备份】任务%d：%s %s\n", i+1, tasks[i].source, tasks[i].destination);
        }

        int num_tasks = sizeof(tasks) / sizeof(tasks[0]); //任务个数
        int num_processes = num_tasks > MAX_PROCESSES ? MAX_PROCESSES : num_tasks;
        pid_t pid[num_tasks];
        sem_t* semaphore;

        //先删除后创建有名信号量
        sem_unlink("my_semaphore");
        semaphore = sem_open("my_semaphore", O_CREAT | O_EXCL, 0644, 1);
        if (semaphore == SEM_FAILED) {
            perror("sem_open");
            exit(EXIT_FAILURE);
        }
        //开启两个进程
        for (int i = 0; i < num_processes; i++) {
            pid[i] = fork();
            
            if (pid[i] == 0) {  //每个进程执行对应任务，开启一个任务线程
                
                sem_wait(semaphore);  // 等待有名信号量
                pthread_mutex_init(&mutex, NULL);

                // 进度条线程
                // 进度条线程的准备
                // 每个进程开始前先清空该变量
                dirSize = 0;
                written = 0;
                dirSize = calculateDirectorySize(tasks[i].source);
                // 进度条线程启动
                pthread_t barThread;
                pthread_create(&barThread, NULL, draw_progress_bar_thread, NULL);

                //任务线程
                pthread_t taskThread;
                printf("\n现在执行【备份任务%d】\n", i+1);
                printf("总共要【备份】的字节数：%lld\n", dirSize);
                pthread_create(&taskThread, NULL, backup_directory_thread, (void*)&tasks[i]);

                pthread_join(taskThread, NULL);
                pthread_join(barThread, NULL);
                printf("已【备份】的字节数：%lld\n", written);
                printf("【备份任务%d】结束\n", i+1);
                sem_post(semaphore);  // 释放有名信号量
                exit(EXIT_SUCCESS);
            } else if (pid[i] < 0) {
                perror("fork");
                exit(EXIT_FAILURE);
            }
        }
        for (int i = 0; i < num_processes; i++ ) {
            wait(NULL);
        }
        // 关闭并删除有名信号量
        pthread_mutex_destroy(&mutex);
        sem_close(semaphore);
        sem_unlink("my_semaphore");

    } else { //sync
        printf("==>输入想要的【同步】任务的个数\n");
        scanf("%d", &num_task);
        printf("按行输入(空格分隔)要执行的【同步】任务的参数【源目录路径，目标目录路径，同步选项（1.B2A，2.A2B 3.AB并集）】\n");
        SyncJob tasks[num_task];
        for (int i = 0; i < num_task; i++) {
            scanf("%s %s %d", tasks[i].source, tasks[i].destination, &tasks[i].mode);
            fflush(stdin);
            printf("【同步】任务%d：%s %s %d\n", i+1, tasks[i].source, tasks[i].destination, tasks[i].mode);
        }

        int num_tasks = sizeof(tasks) / sizeof(tasks[0]);
        int num_processes = num_tasks > MAX_THREADS ? MAX_THREADS : num_tasks;

        pid_t pid[num_tasks]; // 进程号
        sem_t* semaphore; //有名信号量

        //先删除后创建有名信号量
        sem_unlink("my_semaphore");
        semaphore = sem_open("my_semaphore", O_CREAT | O_EXCL, 0644, 1);
        if (semaphore == SEM_FAILED) {
            perror("sem_open");
            exit(EXIT_FAILURE);
        }
        //开启两个进程
        for (int i = 0; i < num_processes; i++) {
            pid[i] = fork();
            
            if (pid[i] == 0) {  //每个进程执行对应任务，开启一个任务线程
                
                sem_wait(semaphore);  // 等待有名信号量
                pthread_mutex_init(&mutex, NULL); //初始化锁

                // 进度条线程
                // 进度条线程的准备
                // 每个进程开始前先清空该变量
                dirSize = 0;
                written = 0;
                //任务线程
                pthread_t taskThread;
                printf("\n现在执行【同步任务%d】\n", i+1);
                pthread_create(&taskThread, NULL, sync_directory_thread, (void*)&tasks[i]);

                pthread_join(taskThread, NULL);
                printf("\n已【同步】的字节数：%lld\n", written);
                printf("【同步任务%d】结束\n", i+1);
                sem_post(semaphore);  // 释放有名信号量
                exit(EXIT_SUCCESS);
            } else if (pid[i] < 0) {
                perror("fork");
                exit(EXIT_FAILURE);
            }
        }
        for (int i = 0; i < num_processes; i++ ) {
            wait(NULL);
        }
        // 关闭并删除有名信号量
        pthread_mutex_destroy(&mutex);
        sem_close(semaphore);
        sem_unlink("my_semaphore");

    }

    return 0;
}